# -*- coding: utf-8 -*-

import json
import logging
import threading
import time
import redis

_redis_host = 'r-bp15mxeode7zyp0lwh.redis.rds.aliyuncs.com'
_redis_port = '6379'
_redis_password='wanlong@2018'


class SessionManager(object):

    # _instance_lock=threading.Lock()

    def __init__(self):
        print 'session init'
        # self.name = 'ljp'
        self.pydis = redis.Redis(host=_redis_host, port=_redis_port,password=_redis_password)
        self._loggle = logging.getLogger(__name__)

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            orig = super(SessionManager, cls)
            cls._instance = orig.__new__(cls, *args, **kwargs)
        return cls._instance

    def addSession(self, key, value, expiration=None):
        self._loggle.info('addSession')
        return self.pydis.set(key, value, ex=expiration)

    def has_key(self, key):
        value = self.pydis.get(key)
        return value == None

    def getSession(self, key):
        return self.pydis.get(key)


session_obj = SessionManager()
